const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    title: "任务记录",
    isTransform: false,
    isBack: true,
    empty: true,
    loading: false,
    bottom: true,
    mission: []
  },
  changeMission(e) {
    let that = this
    console.log(e.currentTarget.dataset.item);
    wx.showModal({
      cancelColor: 'cancelColor',
      title: '确认完成？',
      success: function (res) {
        if (res.confirm) {
          wx.cloud.callFunction({
            name: 'changeMissionList',
            data: {
              id: e.currentTarget.dataset.item
            },
            success: function (res) {
              that.getMissionList()
            },
            fail: function (err) {
              console.log(err);
            }
          })
        } else {

        }
      }
    })

  },
  delMission(e) {
    let that = this
    console.log(e.currentTarget.dataset.item);
    wx.showModal({
      cancelColor: 'cancelColor',
      title: '确认删除？',
      success: function (res) {
        if (res.confirm) {
          wx.cloud.callFunction({
            // 需调用的云函数名
            name: 'delMissionList',
            // 传给云函数的参数
            data: {
              id: e.currentTarget.dataset.item
            },
            success: function (res) {
              console.log(res);
              wx.showToast({
                title: '删除成功',
              })
              that.getMissionList()
            },
            fail: function (err) {
              console.log(err);
            }
          })
        } else {

        }
      }
    })
  },
  getMissionList() {
    let that = this
    wx.cloud.callFunction({
      name: "getOpenid",
      success(res) {
        console.log(res.result.openid);
        wx.cloud.callFunction({
          // 需调用的云函数名
          name: 'getMissionList',
          // 传给云函数的参数
          data: {
            all: true,
            openid: res.result.openid,
          },
          success: function (res) {
            console.log(res.result.data);
            that.setData({
              loading: true,
              bottom: false
            })
            if (res.result.data.length > 0) {
              that.setData({
                mission: res.result.data,
                empty: true
              })
            } else {
              that.setData({
                mission: res.result.data,
                empty: false
              })
            }
          },
          fail: function (err) {
            console.log(err);
          }
        })
      }
    })
    
  },
  onLoad: function (options) {
    let that = this
    that.getMissionList()
  },


})